﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using HoldemGame;

namespace HoldemView
{



    public partial class BoardPanel : Panel
    {
        private IGameState gameState;
        private HoldemTableDirector _director;
        private CardSet newCards=null;

        public IGameState GameState
        {
            get { return gameState;}
            set { 
                gameState=value;
                GameChanged();
            }

        }

        public HoldemTableDirector Director
        { get { return _director; } }

        public BoardPanel(HoldemTableDirector dir)
        {
            _director = dir;

            InitializeComponent();

            this.rtCards.GotFocus += new EventHandler(rtCards_GotFocus);
            this.rtCards.LostFocus += new EventHandler(rtCards_LostFocus);
            this.rtCards.KeyPress += new KeyPressEventHandler(rtCards_KeyPress);
            this.btnCard.Click += new EventHandler(btnCards_Click);
        }

        void btnCards_Click(object sender, EventArgs e)
        {
            UpdateCards(false);
            ToolStripDropDown tsdd =
                CardDropDown.DropDown(rtCards);
            tsdd.Closed += new ToolStripDropDownClosedEventHandler(tsdd_Closed);
        }

        void tsdd_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            ParseCards();
            UpdateCards(true);
        }

        void rtCards_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                BeginInvoke(new EventHandler(delegate(object obj, EventArgs args) { this.Focus(); }));
            }
        }

        void rtCards_LostFocus(object sender, EventArgs e)
        {
            if(!rtCards.ReadOnly)
                ParseCards();
            UpdateCards(true);
        }

        void rtCards_GotFocus(object sender, EventArgs e)
        {
            UpdateCards(false);
        }

        void ParseCards()
        {
            try
            {
                CardSet cs = CardSet.ParseAll(rtCards.Text, 5);
                if(null==newCards)
                    Director.CommitCount++;
                newCards=cs;
            }
            catch (InvalidOperationException ex)
            {
                //MessageBox.Show(ex.ToString());
                Layers.MyLogger.WriteLine("Board: "+ex.ToString());
            }
        }
        public void UpdateBoard()
        {
            GameChanged();
            Refresh();
        }

        public CardSet Cards { get { return newCards != null ? newCards : gameState.Table.BoardCards(); } }

        public void Commit()
        {
            if (null != newCards)
                if(0==Verify(null))
            {
                Director.BoardCards(newCards);
                newCards = null;
                Director.CommitCount--;
            }
        }

        public int Verify(List<Exception> errs)
        {
            if(null!=newCards)
                try{
                    return Director.VerifyBoardCards(newCards,errs);
                }catch(Exception e){
                    if(null!=errs)
                        errs.Add(e);
                    return 1;
                }
            return 0;
        }

        public void GameChanged()
        {
            this.lblInfo.Text = string.Format("{0}@{1}",gameState.Step, HoldemStage.NameOf(gameState.Stage));


            this.rtBank.Text = string.Format("${0}", gameState.Pot);
            this.rtBank.Select(0, -1);
            this.rtBank.SelectionColor = Color.Brown;
            this.rtCards.ReadOnly = !GameState.GameOptions.IsShowAllCards;
            this.btnCard.Enabled = !this.rtCards.ReadOnly;

            CardFormatter.SetBkgr(this,Color.LightBlue);

            UpdateCards(!rtCards.Focused);            
        }

        void UpdateCards(bool useIcon)
        {
            int toShow = gameState.Table.VisibleBoardCardsCount;
            if (GameState.GameOptions.IsShowAllCards)
                toShow = gameState.Table.BoardCards().Count;

            CardFormatter.SetCards(rtCards, Cards, Cards.Count, toShow, useIcon, newCards!=null);
        }

        

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }
    }
}
